// Unit test for the model::attribute oddity
// Copyright 2004 (C) Ralph Thomas

#include <model/any.h>
#include <tests/testCore.h>
#include <util/string.h>
#include <iostream>
#include <vector>

namespace model {
	class testAny : public test::basic {
	  public:
		any testReturn() const {
			return util::string( "foobar" );
		}

		any testReturn2() const {
			std::vector<int> v;

			v.push_back( 54 );
			return v;
		}

		const char* performTest() {
			util::string s = "hellow";
			any i27( 27 );
			any sHellow( s );
			any ccHi( (const char*)"Hi there" );

			if( boost::any_cast<int>( i27 ) != 27 ) return "didn't get 27 back from integer attribute";
			if( boost::any_cast<std::string>( sHellow ) != "hellow" ) return "didn't get string back from attribute";
			if( boost::any_cast<const char*>( ccHi ) != "Hi there" ) return "compiler didn't optimize this const char case";
			//
			// test that assignment works
			//
			sHellow = ccHi = i27;
			if( boost::any_cast<int>( sHellow ) != 27 ) return "assignment from ccHi to sHellow failed";
			if( boost::any_cast<int>( ccHi ) != 27 ) return "assignment from i27 to ccHi failed";
			if( boost::any_cast<int>( i27 ) != 27 ) return "assignment broke i27";
			//
			// test that assignment from a return works
			//
			sHellow = testReturn();
			if( boost::any_cast<std::string>( sHellow ) != "foobar" ) return "didn't get string back from return";
			sHellow = testReturn2();
			std::vector<int> f = boost::any_cast<std::vector<int> >( sHellow );
			if( f.size() != 1 ) return "didn't get vector back from return";
			if( f[0] != 54 ) return "got confused vector back from return";

			return NULL;
		}
		const char* getTestName() { return "testing behavior of model::any"; }
	};
};

void test::runnerSingleton::registerTests() {
	adoptTest( new model::testAny() );
}

